package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.topiclink.TopicLinkConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TopicLinkConfigDaoImpl extends TempDao {
    private static String getTopicLinkConfigSql = "select * from `t_s_topic_link` ORDER BY `activityType` ASC";
    public Map<String,List<TopicLinkConfig>> getTopicLinkConfigMap() {
        Map<String,List<TopicLinkConfig>> topicLinkConfigMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getTopicLinkConfigSql);
            rs = call.executeQuery();
            while (rs.next()) {
                TopicLinkConfig topicLinkConfig = new TopicLinkConfig();
                topicLinkConfig.setAppId(rs.getString("appId"));
                topicLinkConfig.setActivityType(rs.getInt("activityType"));
                topicLinkConfig.setActivityChildType(rs.getInt("activityChildType"));
                topicLinkConfig.setAndroidLink(rs.getString("androidLink"));
                topicLinkConfig.setIosLink(rs.getString("iosLink"));
                topicLinkConfig.setH5Link(rs.getString("h5Link"));
                topicLinkConfigMap.putIfAbsent(topicLinkConfig.getAppId(),new ArrayList<>());
                topicLinkConfigMap.get(topicLinkConfig.getAppId()).add(topicLinkConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return topicLinkConfigMap;
    }

}
